package com.nlp.visualization.controller.spring.controller;

import com.nlp.visualization.pojo.db.TUserSuggestions;
import com.nlp.visualization.service.backEnd.IUserSuggetionsService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LXM
 * @Title: NLPVisualizationTools
 * @Description:
 * @date 2018/5/18下午8:47
 */
@Controller
@RequestMapping("/suggestions")
public class SuggestionsController {


    @Autowired
    private IUserSuggetionsService suggetionsService;


    @RequestMapping("/all")
    @ResponseBody
    public Map getAllSuggestion() {
        Map map = new HashedMap();
        List all = suggetionsService.findAll();
        List no = new ArrayList();
        List no1 = new ArrayList();

        List yes = new ArrayList();
        List yes1 = new ArrayList();

        no = suggetionsService.noDeal();
        yes = suggetionsService.yesDeal();

        for (Object ob : no) {
            Object[] ob1 = (Object[]) ob;
            TUserSuggestions u = new TUserSuggestions();
            u.setType(ob1[2] + "");
            u.setWord(ob1[1] + "");
            u.setTime(Long.parseLong(ob1[3] + ""));
            u.setId(Integer.parseInt(ob1[0] + ""));
            no1.add(u);
        }

        for (Object ob : yes) {
            Object[] ob1 = (Object[]) ob;
            TUserSuggestions u = new TUserSuggestions();
            u.setType(ob1[2] + "");
            u.setWord(ob1[1] + "");
            u.setTime(Long.parseLong(ob1[3] + ""));
            u.setId(Integer.parseInt(ob1[0] + ""));
            yes1.add(u);
        }

        map.put("code", 0);
        map.put("all", all);
        map.put("yes", yes1);
        map.put("no", no1);
        return map;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map delete(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id") + "");
        Map map = new HashedMap();

        try {
            suggetionsService.deleteById(id);
            map.put("code", 0);
        } catch (Exception e) {
            map.put("code", 999);
            e.printStackTrace();
        }
        return map;
    }


    @RequestMapping("/mark")
    @ResponseBody
    public Map mark(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id") + "");
        Map map = new HashedMap();

        try {
            TUserSuggestions suggestions = suggetionsService.findById(id);
            String n = suggestions.getType().replace("【未处理】", "【已处理】");
            suggestions.setType(n);
            suggetionsService.update(suggestions);
            map.put("code", 0);
        } catch (Exception e) {
            map.put("code", 999);
            e.printStackTrace();
        }
        return map;
    }

}
